package gatperdut.chato.core.interpreter.actions.generic;

import gatperdut.chato.constants.CSConstantsCommandResult.CommandExecutionResult;
import gatperdut.chato.core.characters.BaseController;
import gatperdut.chato.core.interpreter.grammar.comminfo.CommandInfo;
import gatperdut.chato.core.interpreter.grammar.comminfo.indexedword.IndexedWord;
import gatperdut.chato.core.item.ItemController;

import java.util.List;

public abstract class Action {

    private final BaseController entityController;

    private final CommandInfo info;

    private String replacedEmote = null;

    public Action( CommandInfo info, BaseController entityController ) {
        this.entityController = entityController;
        this.info = info;
    }

    public String getLastPartEmote() {
        if ( getInfo().getEmote() != null ) {
            String emote = getReplacedEmote();
            if ( emote == null ) {
                return null;
            }
            return emote;
        }
        else {
            return "";
        }
    }

    public abstract CommandExecutionResult perform();

    public boolean emptyEmote() {
        boolean emoteNotNull = info.getEmote() != null;
        if ( !emoteNotNull ) {
            return false;
        }
        boolean emoteIsEmpty = info.getEmote().matches( "[' ' | '\t']+" ) || info.getEmote().equals( "" );
        if ( emoteNotNull && emoteIsEmpty && info.getiWords().isEmpty() ) {
            return true;
        }
        else {
            return false;
        }
    }

    public void checkForRunningCraft() {
        if ( getEntityController().getCraftThread() != null ) {
            if ( interruptsCrafting() ) {
                getEntityController().stopCraftThread();
            }
        }
    }

    public String getReplacedEmote() {
        if ( replacedEmote == null ) {
            if ( getInfo().getEmote() != null ) { // valid, then
                if ( !getInfo().getiWords().isEmpty() ) {
                    setIWordsReplacements( getInfo().getiWords() );
                    for ( IndexedWord iw : getInfo().getiWords() ) { // --check
                                                                     // everything
                                                                     // was
                                                                     // replaced
                                                                     // correctly.
                        if ( iw.getReplacement() == null ) {
                            return null;
                        }
                    }
                    String[] replacements = getInfo().getReplacements();
                    replacedEmote = getInfo().replaceIWords( replacements );
                }
                else {
                    replacedEmote = getInfo().getEmote();
                }
            }
        }

        return replacedEmote;
    }

    private void setIWordsReplacements( List<IndexedWord> iws ) throws NullPointerException {

        for ( IndexedWord iw : iws ) {
            switch ( iw.getRefType() ) {
            case ENTITY:
                BaseController ec = getEntityController().getRoomController().getCharactersController().getEntityController( iw );
                if ( ec != null ) {
                    iw.setReplacement( ec.getCharacter().getShortDesc() );
                }
                break;
            case OBJECT:
                ItemController ic = getEntityController().getInventoryController().getItemController( iw );
                if ( ic == null ) {
                    ic = getEntityController().getRoomController().getInventoryController().getItemController( iw );
                }
                if ( ic != null ) {
                    iw.setReplacement( ic.getShortDesc() );
                }
                break;
            case UNKNOWN:
                break;
            default:
                throw new IllegalArgumentException( "Indexed word with reftype UNKNOWN while getting IWords replacements." );
            }
        }
    }

    public boolean checkUnindexedWord( IndexedWord iw ) {
        if ( iw.getIndex() != 1 ) {
            getEntityController().tx( "You must be holding that item in your hands - you cannot index it." );
            return false;
        }
        return true;
    }

    protected ItemController findInRoomOrInventory( IndexedWord iw, boolean ground ) {
        ItemController ic = null;
        if ( !ground ) {
            ic = getEntityController().getInventoryController().getItemController( iw );
        }
        if ( ground || ( !ground && ic == null ) ) {
            ic = getEntityController().getRoomController().getInventoryController().getItemController( iw );

        }

        return ic;
    }

    public abstract boolean interruptsCrafting();

    public CommandInfo getInfo() {
        return info;
    }

    public BaseController getEntityController() {
        return entityController;
    }
}
